﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace GeodeticCoordinates
{
    internal class operateFiles
    {
        public static int readFileLines(string path)  //这里的参数是txt所在路径
        {
            int lines = 0;  //用来统计txt行数
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            while (sr.ReadLine() != null)
            {
                lines++;
            }

            fs.Close();
            sr.Close();

            return lines;

        }

        /// <summary>
        /// 导入txt文本
        /// </summary>
        /// <returns>字符串二维数组</returns>
        public string[][] AddFiles()
        {
            string[] str2 = new string[999];
            string[][] data = new string[99][];

            OpenFileDialog oFD1 = new OpenFileDialog();
            oFD1.InitialDirectory = "C:\\";   //显示初始目录，
            oFD1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";   // 文件名筛选字符串
            oFD1.FilterIndex = 2;
            oFD1.RestoreDirectory = true;
            //若用户在对话框中单击确定，则结果为DialogResult.OK
            //否则结果为DialogResult.Cancel

            if (oFD1.ShowDialog() == DialogResult.OK)       //打开文件
            {

                MessageBox.Show("成功打开文件：" + oFD1.FileName);    //显示选择的文件名
                FileStream fs = new FileStream(oFD1.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  //需添加头文件system.IO
                StreamReader s1 = new StreamReader(fs, Encoding.ASCII);

                int lines = readFileLines(oFD1.FileName);
                data = new string[lines][];

                string sepatator = ",";  //以逗号分割字符串
                char[] cgap = sepatator.ToCharArray();

                for (int i = 0; i < lines; i++)
                {
                    string str1 = s1.ReadLine();   //读取一行字符
                    if (str1 == null) break;
                    str2 = str1.Split(cgap, StringSplitOptions.None); //基于数组中的字符将字符串拆分为多个子字符串
                    data[i] = str2;
                }
            }
            return data;
        }

        /// <summary>
        /// 导出txt文本成果数据
        /// </summary>
        /// <param name="i">listview中的行数</param>
        /// <param name="points">所有点的数据成果</param>
        public void ExportFiles(int i, string[,] points)
        {
            // 打开文件夹并进行命名存储txt
            SaveFileDialog sFD1 = new SaveFileDialog();
            sFD1.InitialDirectory = "C:\\";
            sFD1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";  //保存文件的格式
            sFD1.FilterIndex = 1;
            sFD1.RestoreDirectory = true;

            if (sFD1.ShowDialog() == DialogResult.OK)
            {
                string s = "";
                for (int m = 0; m < points.GetLength(0); m++)  //所有项的集合数
                {
                    for (int n = 0; n < points.GetLength(1); n++)   // 一个项的所有子项集合数 
                    {
                        if (points[m, n].ToString() == "")
                        {
                            break;
                        }

                        s += points[m, n] + ",";
                    }
                    s = s.Remove(s.LastIndexOf(","), 1); // 去除末尾最后一个逗号
                    s += "\r\n";
                }

                using (StreamWriter sw = new StreamWriter(sFD1.FileName))
                {
                    sw.WriteLine(s);   //在文件中添加文本
                }
                MessageBox.Show("成功保存文件：" + sFD1.FileName);
            }
        }
    }
}
